/**
 * 
 */
package android.pot.com.smartcar.widget.flowTag;

import android.content.Context;
import android.pot.com.smartcar.R;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kince
 * @category ģ������Ӧ�õײ�tagview
 * 
 */
public class TagF2ListView extends FlowsLayout implements OnClickListener {

	private boolean mIsDeleteMode;
	private OnTagCheckedChangedListener mOnTagCheckedChangedListener;
	private OnTagClickListener mOnTagClickListener;
	private int mTagViewBackgroundResId;
	private int mTagViewTextColorResId;
	private final List<Tag> mTags = new ArrayList<Tag>();
	private Map<String,TagView> map = new HashMap<>();
	/**
	 * @param context
	 */
	public TagF2ListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * @param context
	 * @param attributeSet
	 */
	public TagF2ListView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * @param context
	 * @param attributeSet
	 * @param defStyle
	 */
	public TagF2ListView(Context context, AttributeSet attributeSet, int defStyle) {
		super(context, attributeSet, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	@Override
	public void onClick(View v) {
		if ((v instanceof TagView)) {
			Tag localTag = (Tag) v.getTag();
			if (this.mOnTagClickListener != null) {
				this.mOnTagClickListener.onTagClick((TagView) v, localTag);
			}
		}
	}

	private void init() {

	}

	private void inflateTagView(final Tag t, boolean b) {

		TagView localTagView = (TagView) View.inflate(getContext(),
				R.layout.tag, null);
		localTagView.setText(t.getTitle());
		localTagView.setTag(t);
		map.put(t.getTitle(),localTagView);
		if (t.isChecked()){
			int c = getResources().getColor(R.color.white);
			localTagView.setTextColor(c);
			localTagView.setBackgroundResource(R.drawable.shape_corner_stoke_d8_bg);
		}else{
			localTagView.setTextColor(getResources().getColor(R.color.gray_666666));
			localTagView.setBackgroundResource(R.drawable.corner_f2_bg);
		}

		localTagView.setChecked(false);
		localTagView.setCheckEnable(false);
		if (mIsDeleteMode) {
			int k = (int) TypedValue.applyDimension(1, 5.0F, getContext()
					.getResources().getDisplayMetrics());
			localTagView.setPadding(localTagView.getPaddingLeft(),
					localTagView.getPaddingTop(), k,
					localTagView.getPaddingBottom());
//			localTagView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
//					R.drawable.forum_tag_close, 0);
		}

		localTagView.setOnClickListener(this);

		addView(localTagView);
	}

	public void addTag(int i, String s) {
		addTag(i, s, false);
	}

	public void addTag(int i, String s, boolean b) {
		addTag(new Tag(i, s), b);
	}

	public void addTag(Tag tag) {
		addTag(tag, false);
	}

	public void addTag(Tag tag, boolean b) {
		mTags.add(tag);
		inflateTagView(tag, b);
	}

	public void addTags(List<Tag> lists) {
		addTags(lists, false);
	}

	public void addTags(List<Tag> lists, boolean b) {
		for (int i = 0; i < lists.size(); i++) {
			addTag((Tag) lists.get(i), b);
		}
	}

	public void CheckedTag(){
			for (Tag t:mTags){
				TagView view = map.get(t.getTitle());
				if (t.isChecked()){
					int c = getResources().getColor(R.color.white);
					view.setTextColor(c);
					view.setBackgroundResource(R.drawable.shape_corner_stoke_d8_bg);
				}else{
					view.setTextColor(getResources().getColor(R.color.gray_666666));
					view.setBackgroundResource(R.drawable.corner_f2_bg);
				}
			}

	}

	public List<Tag> getTags() {
		return mTags;
	}

	public View getViewByTag(Tag tag) {
		return findViewWithTag(tag);
	}

	public void removeTag(Tag tag) {
		mTags.remove(tag);
		removeView(getViewByTag(tag));
	}

	public void setDeleteMode(boolean b) {
		mIsDeleteMode = b;
	}

	public void setOnTagCheckedChangedListener(
			OnTagCheckedChangedListener onTagCheckedChangedListener) {
		mOnTagCheckedChangedListener = onTagCheckedChangedListener;
	}

	public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
		mOnTagClickListener = onTagClickListener;
	}

	public void setTagViewBackgroundRes(int res) {
		mTagViewBackgroundResId = res;
	}

	public void setTagViewTextColorRes(int res) {
		mTagViewTextColorResId = res;
	}

	public void setTags(List<? extends Tag> lists) {
		setTags(lists, false);
	}

	public void setTags(List<? extends Tag> lists, boolean b) {
		removeAllViews();
		mTags.clear();
		if (lists!=null&& lists.size()>0) {
			for (int i = 0; i < lists.size(); i++) {
				addTag((Tag) lists.get(i), b);
			}
		}
	}

	public static abstract interface OnTagCheckedChangedListener {
		public abstract void onTagCheckedChanged(TagView tagView, Tag tag);
	}

	public static abstract interface OnTagClickListener {
		public abstract void onTagClick(TagView tagView, Tag tag);
	}

}
